package com.hp.jdoss.config;


import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;


/**
 * @author dongxing
 */
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties({OssProperties.class, SysTemProxyConfig.class})
@ConditionalOnProperty(prefix = "jdoss", name = "enable", havingValue = "true")
public class JdossConfiguration {

    private OssProperties ossProperties;
    private SysTemProxyConfig sysTemProxyConfig;

    @Bean
    @ConditionalOnMissingBean
    public AmazonS3 amazonS3() {

        if (Optional.ofNullable(sysTemProxyConfig.getEnable()).isPresent() && sysTemProxyConfig.getEnable()) {
            System.setProperty("http.proxySet", String.valueOf(sysTemProxyConfig.getEnable()));
            System.setProperty("http.proxyHost", String.valueOf(sysTemProxyConfig.getUrl()));
            System.setProperty("http.proxyPort", String.valueOf(sysTemProxyConfig.getPort()));
            System.setProperty("https.proxySet", String.valueOf(sysTemProxyConfig.getEnable()));
            System.setProperty("https.proxyHost", String.valueOf(sysTemProxyConfig.getUrl()));
            System.setProperty("https.proxyPort", String.valueOf(sysTemProxyConfig.getPort()));
        }
        ClientConfiguration config = new ClientConfiguration();
        AwsClientBuilder.EndpointConfiguration endpointConfig =
                new AwsClientBuilder.EndpointConfiguration(ossProperties.getEndpoint(), ossProperties.getRegion());
        AWSCredentials awsCredentials = new BasicAWSCredentials(ossProperties.getAccessKey(), ossProperties.getSecretKey());
        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
        AmazonS3 s3 = AmazonS3Client.builder()
                .withEndpointConfiguration(endpointConfig)
                .withClientConfiguration(config)
                .withCredentials(awsCredentialsProvider)
                .disableChunkedEncoding()
                .withPathStyleAccessEnabled(true)
                .build();
        return s3;
    }

    @Bean
    @ConditionalOnMissingBean(JdossTemplate.class)
    @ConditionalOnBean({AmazonS3.class})
    public JdossTemplate jdossTemplate(OssProperties ossProperties, AmazonS3 amazonS3) {
        return new JdossTemplate(ossProperties, amazonS3);
    }

}